﻿// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

namespace BDDEditor
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ICSharpCode.AvalonEdit.CodeCompletion;
    using System.IO;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class BDDLoader
    {
        private Dictionary<string, string> completionDataDcitionary;
        public List<ICompletionData> completionList;
        public string FeauteFolder { get; set; }

        public BDDLoader()
        {
            FeauteFolder = @".\Features";
            completionDataDcitionary = new Dictionary<string, string>();
            completionList = new List<ICompletionData>(){
                            new CompletionData("Scenario"),
                            new CompletionData("Feature"),
                            new CompletionData("When "),
                            new CompletionData("Then "),
                            new CompletionData("Given "),
                            new CompletionData("And "),
                            new CompletionData("But "),
                            new CompletionData("Scenario Outline"),
                            new CompletionData("Examples"),
                            new CompletionData("Background") 
                            };
            LoadDataFromFiles();
            
        }

        public void LoadDataFromFiles()
        {
            DirectoryInfo drInfo = new DirectoryInfo(FeauteFolder);
            FileInfo[] fileInfo = drInfo.GetFiles("*.feature", SearchOption.AllDirectories);
            FileInfo[] fileInfo2 = drInfo.GetFiles("*.txt", SearchOption.AllDirectories);
            var fileInfoList = fileInfo.Union(fileInfo2);
            foreach (var item in fileInfoList)
            {
                TextReader txrdr = new StreamReader(item.FullName);
                string text = txrdr.ReadLine();
                do
                {
                    text = text.Trim();
                    if (!completionDataDcitionary.ContainsKey(text) && !string.IsNullOrEmpty(text))
                    {
                        completionDataDcitionary.Add(text, text);
                        completionList.Add(new CompletionData(text));
                        //add substrings to list
                        string[] substrings = text.Split(' ');
                        foreach (var item2 in substrings)
                        {
                            if (!completionDataDcitionary.ContainsKey(item2) && !string.IsNullOrEmpty(item2))
                            {
                                completionDataDcitionary.Add(item2, item2);
                                completionList.Add(new CompletionData(item2));
                            }
                        }
                    }
                    
                    text = txrdr.ReadLine();
                } while (text != null);
                txrdr.Close();
            }
        }
    }
}
